package org.dromara.system.utils;

import cn.hutool.core.io.FileUtil;
import cn.hutool.extra.qrcode.QrCodeUtil;
import cn.hutool.extra.qrcode.QrConfig;
import jakarta.annotation.PostConstruct;
import lombok.NoArgsConstructor;
import org.dromara.common.core.utils.JudgeUtils;
import org.dromara.system.domain.vo.SysOssVo;
import org.dromara.system.service.ISysOssService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.File;
import java.util.UUID;

import static lombok.AccessLevel.PRIVATE;

/**
 * 扩展 hutool TreeUtil 封装系统树构建
 *
 * @author Lion Li
 */
@Component
@NoArgsConstructor(access = PRIVATE)
public class QrCodeUtils {

    @Autowired
    private ISysOssService ossService;
    private static ISysOssService staticOssService;

    @PostConstruct
    public void init() {
        staticOssService = ossService;
    }

    /**
     * 获取二维码
     *
     * @param info     二维码内容
     * @param logoPath logo路径
     * @return 二维码地址
     */
    public static SysOssVo getCode(String info, String logoPath) {
        QrConfig config = new QrConfig(300, 300);
/*        // 设置边距，既二维码和背景之间的边距
        config.setMargin(3);
        // 设置前景色，既二维码颜色（青色）
        config.setForeColor(Color.CYAN.getRGB());
        // 设置背景色（灰色）
        config.setBackColor(Color.GRAY.getRGB());*/
        //设置logo
        JudgeUtils.conditionNull(logoPath).notNullHandle(t -> config.setImg(t));
        File file = QrCodeUtil.generate(info, config, FileUtil.file("./temp/" + UUID.randomUUID() + ".jpg"));
        SysOssVo upload = staticOssService.upload(file);
        FileUtil.del(file);
        return upload;
    }

}
